package com.huizu.project.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.enums.BusinessType;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.common.core.page.TableDataInfo;

/**
 * 用户学院权限Controller
 * 
 * @author zhangmx
 * @date 2024-06-26
 */
@RestController
@RequestMapping("/project/sysuserauth")
public class SysUserAuthController extends BaseController
{
    @Autowired
    private ISysUserAuthService sysUserAuthService;

    /**
     * 查询用户学院权限列表
     */
    @PreAuthorize("@ss.hasPermi('project:sysuserauth:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserAuth sysUserAuth)
    {
        startPage();
        List<SysUserAuth> list = sysUserAuthService.selectSysUserAuthList(sysUserAuth);
        return getDataTable(list);
    }

    /**
     * 导出用户学院权限列表
     */
    @PreAuthorize("@ss.hasPermi('project:sysuserauth:export')")
    @Log(title = "用户学院权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserAuth sysUserAuth)
    {
        List<SysUserAuth> list = sysUserAuthService.selectSysUserAuthList(sysUserAuth);
        ExcelUtil<SysUserAuth> util = new ExcelUtil<SysUserAuth>(SysUserAuth.class);
        util.exportExcel(response, list, "用户学院权限数据");
    }

    /**
     * 获取用户学院权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:sysuserauth:query')")
    @GetMapping(value = "/{userId}/{type}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId,@PathVariable("type") String type)
    {
        return success(sysUserAuthService.selectSysUserAuthByUserId(userId,type));
    }

    /**
     * 新增用户学院权限
     */
    @PreAuthorize("@ss.hasPermi('project:sysuserauth:add')")
    @Log(title = "用户学院权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserAuth sysUserAuth)
    {
        return toAjax(sysUserAuthService.insertSysUserAuth(sysUserAuth));
    }

    /**
     * 修改用户学院权限
     */
    @PreAuthorize("@ss.hasPermi('project:sysuserauth:edit')")
    @Log(title = "用户学院权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserAuth sysUserAuth)
    {
        return toAjax(sysUserAuthService.updateSysUserAuth(sysUserAuth));
    }

    /**
     * 删除用户学院权限
     */
    @PreAuthorize("@ss.hasPermi('project:sysuserauth:remove')")
    @Log(title = "用户学院权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(sysUserAuthService.deleteSysUserAuthByUserIds(userIds));
    }

    /**
     * 获取树列表
     * @return
     */
    @PostMapping("/listTree")
    public AjaxResult listTree(Map<String,Object> map){
        return sysUserAuthService.listTree(map);
    }

    /**
     * 用户拥有的节点权限
     * @param map
     * @return
     */
    @PostMapping("/listUserAuth")
    public AjaxResult listUserAuth(@RequestBody Map<String,Object> map){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        return sysUserAuthService.listUserAuth(map,userId);
    }

    /**
     * 提交权限
     * @return
     */
    @PostMapping("/subAuth")
    public AjaxResult subAuth(@RequestBody Map<String,Object> map){
        return sysUserAuthService.subAuth(map);
    }
}
